package xyz.app.dependencies;

import jakarta.persistence.metamodel.SingularAttribute;
import lombok.NonNull;
import xyz.app.AbstractDomain;
import xyz.app.Dao;

import java.util.AbstractMap;

public class OwnerEntry<T extends AbstractDomain, V extends AbstractDomain>
        extends AbstractMap.SimpleEntry<SingularAttribute<? super T, V>, Dao<T>> {

    private final boolean removable;

    private OwnerEntry(SingularAttribute<? super T, V> attribute, Dao<T> router, boolean removable) {
        super(attribute, router);
        this.removable = removable;
    }

    public static <T extends AbstractDomain, V extends AbstractDomain> OwnerEntry<T, V> of(@NonNull SingularAttribute<? super T, V> attribute, @NonNull Dao<T> router) {
        return new OwnerEntry<>(attribute, router, false);
    }

    public static <T extends AbstractDomain, V extends AbstractDomain> OwnerEntry<T, V> removable(@NonNull SingularAttribute<? super T, V> attribute, @NonNull Dao<T> router) {
        return new OwnerEntry<>(attribute, router, true);
    }

    public String getName() {
        return getKey().getName();
    }

    public Dao<T> getDao() {
        return getValue();
    }

    public boolean isRemovableFrom(@NonNull Class<? extends AbstractDomain> domainClass) {
        return removable && getKey().getJavaType().isAssignableFrom(domainClass);
    }
}